package org.ytor.core.sysapi.file.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ytor.common.enums.MimeType;
import org.ytor.core.DownloadMapper;
import org.ytor.core.model.Person;
import org.ytor.core.util.RespUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * created by yangtong on 2025/5/30 20:55:57
 * <br/>
 */
@RestController
@RequestMapping("/sys/fileTest")
@Tag(name = "文件测试")
public class FileTestController {

    /**
     * 将List解析成Excel表格
     */
    @DownloadMapper(value = "getXlsx", filename = "这是一个测试文件.xlsx", type = Person.class, mime = MimeType.APPLICATION_XLSX)
    @Operation(summary = "getXlsx", description = "将List解析成Excel表格")
    public List<Person> getXlsx() {
        List<Person> people = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Person person = new Person();
            person.setId(i);
            person.setAge(i * 10);
            person.setName("张王李" + i);
            person.setHobby("唱跳rap篮球" + i);
            person.setPhone("13891" + i + "213" + i);
            person.setRemark("哈哈哈哈");
            person.setTest("吼吼吼");
            person.setBirthday("2012-12-12");
            people.add(person);
        }

        return people;
    }

    /**
     * 预览图片
     */
    @DownloadMapper(value = "previewJpeg", filename = "测试文件.jpg", mime = MimeType.IMAGE_JPEG)
    @Operation(summary = "previewJpeg", description = "预览文件")
    public InputStream previewJpeg() throws FileNotFoundException {
        File file = new File("C:\\Users\\az\\Pictures\\动漫美女 鲜花 帽子 唯美 好看 4K壁纸_彼岸图网.jpg");
        RespUtil.setContentLength(file.length());
        return new FileInputStream(file);
    }
}
